/*
 *  main.cpp
 *  atto
 *
 *  Created by Russell Webb on 16/02/06.
 *
 * ---------------------- Build Instructions ----------------------------------------------------------------
 * 
 * To build an application called 'app'.
 *     gcc main.cpp atto.cpp atto_internals.cpp GameItems.cpp Item.cpp World.cpp Vec2.cpp -Wall -lm -lstdc++ `fltk-config --ldflags --use-images` -o app
 * 
 * On MacOS X you also need to add a proper bundle.
 *     /Developer/Tools/Rez -t APPL -o app /usr/local/include/FL/mac.r
 * 
 */
 
// Set the debug level here.
// For development it should be CHECK_DEBUG_LEVEL.
#define DEBUG_LEVEL CHECK_DEBUG_LEVEL

#include <iostream>
#include <fstream>
#include <time.h>
#include "atto.h"
#include "VmGroup.h"
#include "Compiler.h"
#include "GameSystem.h"

using namespace Vorpal;

using namespace std;

GameSystem *sys;

void key(int code){
	double arg[] = { code };
	sys->Run("key", arg, sizeof(arg)/sizeof(*arg));
}

// The typical mouse action by the user will begin with a mouse_down call, 
// include 0 or more mouse_drag calls, and end with a mouse_up call.

void mouse_down(int x, int y, int button){
	double arg[] = { x, y, button };
	sys->Run("down", arg, sizeof(arg)/sizeof(*arg));
}

void mouse_drag(int x, int y, int button){
	double arg[] = { x, y, button };
	sys->Run("drag", arg, sizeof(arg)/sizeof(*arg));
}

void mouse_up(int x, int y, int button){
	double arg[] = { x, y, button };
	sys->Run("up", arg, sizeof(arg)/sizeof(*arg));
}

void idle(void *custom){
	TheGroup().RoundRobin();
	atto_timeout(1.0/20.0, idle, 0);
}

void draw(){
	static bool started = false;
	if(!started){
		atto_timeout(1.0/20.0, idle, 0);
		started = true;
	}
	atto_color(0.0, 0.0, 0.0);
	atto_fill(0, 0, 600, 600);

	atto_color(1.0, 0.0, 0.0);
	sys->Run("draw", 0, 0);
}

void help(){
	cout << "*************************************" << endl;
	cout << "*********** Tulgey Help *************" << endl;
	cout << "*************************************" << endl << endl;
	cout << "Usage: tulgy source_path" << endl << endl;
	
	cout << "*************************************" << endl;
}

int main(int argc, char **argv) {
	if(argc < 2){
		help();
		exit(1);
	}	
	cout << "Whiffling through the tulgey wood:\n";
	sys = new GameSystem(argv[1]);

	uint numThreads = 4;
	tp_init(numThreads);

	srand(time(0));
	atto_setup(600, 600, key, mouse_down, mouse_drag, mouse_up, draw);
	atto_main(1, argv);

	delete sys;
	tp_done();
}

